package com.qingxin.park.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.qingxin.park.entity.CropsType;
import com.qingxin.park.entity.Cropsjg;
import com.qingxin.park.entity.CropsjgType;

import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.UploadFile;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.ui.bean.TreeNode;

/**
 * 农作物
 * @author Administrator
 *
 */
@Service
public class CropsjgService extends BaseService<Cropsjg, String> {

	/**
	 * 获取作物数据
	 * @param parkid
	 * @param typeid
	 * @param isParent
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<TreeNode> getUITreeData(String parkid, String typeid, boolean isParent){
		List<TreeNode> list = new ArrayList<TreeNode>();
		
		StringBuilder hql = new StringBuilder();
		hql.append("select new com.qingxin.park.entity.Cropsjg(c.cropid, c.cropName)");
		hql.append(" from Cropsjg c where c.cropsjgType.typeid=?");
		hql.append(" order by c.cropName asc");
		List<Cropsjg> crops = listByHql(hql.toString(), typeid);
		if(crops != null){
			for(Cropsjg c : crops){
				list.add(new TreeNode(c.getCropid(), c.getCropName(), "CROP", null, isParent));
			}
		}
		return list;
	}
	
	/**
	 * 获取作物全名：分类名 > 作物名
	 * @param cropid
	 * @return
	 */
	public String getCropFullname(String cropid){
		Cropsjg crops = get(cropid);
		if(crops != null){
			return crops.getCropsjgType().getTypeName() +" > "+ crops.getCropName();
		}
		return null;
	}
	
	
	/**
	 * 列表数据
	 * @param parkid
	 * @param typeid
	 * @param cropName
	 * @param pageParam
	 * @param loginUser
	 * @return
	 */
	public Pager list(String parkid, String typeid, String cropName, PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("select new com.qingxin.park.entity.Cropsjg(" +
				"c.cropid, c.cropCode, c.cropName, c.units, c.createTime, t.typeName)" +
				" from Cropsjg c, CropsjgType t where c.cropsjgType.typeid=t.typeid");
		
		if(StringUtils.isNotEmpty(typeid)){
			querier.append(" and c.cropsjgType.typeid=:typeid");
			querier.setParam("typeid", typeid);
		}
		if(StringUtils.isNotEmpty(cropName)){
			cropName = cropName.replace("'", "");
			querier.append(" and (c.cropName like :cropName or c.cropCode like :cropCode) ");
			querier.setParam("cropName", "%"+cropName+"%");
			querier.setParam("cropCode", "%"+cropName+"%");
		}
		
		querier.append(pageParam.getOrderby(" order by c.cropCode asc", "c"));
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	/**
	 * 添加农作物信息
	 * @param crops
	 * @param typeid
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(Cropsjg crops, String typeid, LoginUser loginUser, HttpServletRequest request){
		crops.setCropsjgType(new CropsjgType(typeid));
		
		Date now = new Date();
		crops.setCreateTime(now);
		crops.setUpdateTime(now);
		crops.setCreateBy(loginUser.getLogin_name());
		crops.setUpdateBy(loginUser.getLogin_name());
		
		if(StringUtils.isNotEmpty(crops.getCropImg())){
			UploadFile.moveTmpTo(crops.getCropImg(), UploadFile.DIR_CROP_IMG);
		}
		
		this.save(crops);
		return null;
	}
	
	public String getCropCodeMax(){
		String cropCode = (String) getByHql("select max(cropCode) from Cropsjg");
		return cropCode;
	}
	
	
	/**
	 * 修改农作物信息
	 * @param crops
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(Cropsjg crops, LoginUser loginUser, HttpServletRequest request){
		Cropsjg old = getSafeEntity(crops.getCropid());
		if(old == null){
			return new SysError("参数错误");
		}
		
		if(StringUtils.isNotEmpty(crops.getCropImg()) && !crops.getCropImg().equals(old.getCropImg())){
			UploadFile.moveTmpTo(crops.getCropImg(), UploadFile.DIR_CROP_IMG);
		}
		
		String fields = "cropName,units,cropImg,remark";
		BeanUtil.setTargetFields(old, crops, fields, true);
		
		old.setUpdateBy(loginUser.getLogin_name());
		old.setUpdateTime(new Date());
		
		Updater<Cropsjg> updater = new Updater<>(old, fields+",updateBy,updateTime");
		this.update(updater);
		
		return null;
		
	}
	
	
	/**
	 * 删除农作物信息
	 * @param cropids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public Object delete(String[] cropids, LoginUser loginUser, HttpServletRequest request){
		for(String cropid : cropids){
			Cropsjg crops = get(cropid);
			if(crops==null){
				continue;
			}
			
			this.delete(crops);
		}
		
		return null;
	}
	
	
	
}
